//
//  SEExamineRecordListCell.swift
//  SmartEyeForPatient
//
//  Created by qdsg on 2021/4/8.
//

import UIKit

enum SEExamineRecordListCellActionType {
    case ai
    case report
}

protocol SEExamineRecordListCellDelegate: NSObjectProtocol {
    func examineRecordListCellAIButtonAction(indexPath: IndexPath, actionType: SEExamineRecordListCellActionType)
}

class SEExamineRecordListCell: UITableViewCell {

    private var indexPath = IndexPath(row: 0, section: 0)
    weak var delegate: SEExamineRecordListCellDelegate?

    override func awakeFromNib() {
        super.awakeFromNib()
        backgroundColor = .white
        contentView.backgroundColor = .white
    }
    
    func updateWithViewModel(_ viewModel: SEExamineDetailViewModel, _ indexPath: IndexPath) {
        self.indexPath = indexPath
        nameLabel.text = viewModel.model.name
        timeLabel.text = viewModel.model.checkScheduleTime
        positionLabel.text = viewModel.model.checkEyePart
        hospitalLabel.text = viewModel.model.checkHosName
        aiLogoImgView.isHidden = viewModel.isHiddenAiLogo
        AIButton.isHidden = viewModel.isHiddenAiBtn
        reportButton.isHidden = viewModel.isHiddenReportBtn
        collectionView.titles = viewModel.checkItemNamesArray

        frame = UIScreen.main.bounds
        layoutIfNeeded()
        collectionView.reloadData()
        let contentSize = collectionView.collectionViewLayout.collectionViewContentSize
        collectionViewHeightConstraint.constant = contentSize.height
    }
    
    @IBOutlet private var collectionViewHeightConstraint: NSLayoutConstraint!
    
    @IBOutlet private weak var nameLabel: UILabel!
    @IBOutlet private weak var timeLabel: UILabel!
    @IBOutlet private weak var positionLabel: UILabel!
    @IBOutlet private weak var hospitalLabel: UILabel!
    @IBOutlet private weak var collectionView: SETagsCollectionView!
    @IBOutlet weak var aiLogoImgView: UIImageView!

    @IBOutlet weak var AIButton: UIButton! {
        didSet {
            AIButton.layer.cornerRadius = AIButton.height / 2.0
            AIButton.layer.borderWidth = 1
            AIButton.layer.borderColor = UIColor(hex: "#20D1A3").cgColor
            AIButton.layer.masksToBounds = true
        }
    }

    @IBAction func AIButtonAction(_ sender: UIButton) {
        delegate?.examineRecordListCellAIButtonAction(indexPath: indexPath, actionType: .ai)
    }

    @IBOutlet weak var reportButton: UIButton! {
        didSet {
            reportButton.layer.cornerRadius = AIButton.height / 2.0
            reportButton.layer.masksToBounds = true
        }
    }

    @IBAction func reportButtonAction(_ sender: UIButton) {
        delegate?.examineRecordListCellAIButtonAction(indexPath: indexPath, actionType: .report)
    }
}
